package com.calenda.setevent.activity;

import com.calenda.setevent.R;
import com.calenda.setevent.api.BaseAjaxCallBack.IAjaxCallbackMMK;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import static com.calenda.setevent.api.SetEventApi.*;

public class RegisterActivity extends Activity implements IAjaxCallbackMMK {

	private EditText edtUserName;
	private EditText edtPassword;
	private EditText edtFullName;
	private Button btnSex;
	private ImageView btRegister;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_activity);

		edtUserName = (EditText) this.findViewById(R.id.edtUserName);
		edtPassword = (EditText) this.findViewById(R.id.edtPassword);
		edtFullName = (EditText) this.findViewById(R.id.edtFullName);
		btRegister = (ImageView) this.findViewById(R.id.btRegister);
		btRegister.setOnClickListener(mOnClickListener);

		btnSex = (Button) this.findViewById(R.id.btnSex);
		btnSex.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showSexDialog();
			}
		});

	}

	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			String username = edtUserName.getText().toString().trim();
			String pwd = edtPassword.getText().toString().trim();
			String name = edtFullName.getText().toString().trim();
			String sex = btnSex.getText().toString().trim();

			if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(pwd)
					&& !TextUtils.isEmpty(name)){
				register(username, pwd, name, sex, RegisterActivity.this,
						RegisterActivity.this);
			}else{
				Toast.makeText(RegisterActivity.this, "Các trường không được để trống !", Toast.LENGTH_LONG)
				.show();
			}
				
		}
	};

	private void showSexDialog() {
		final CharSequence[] items = { "Nam", "Nữ" };

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Giới tính");
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				// Do something with the selection
				btnSex.setText(items[item]);
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public <T> void handler(T t, int code) {
		// TODO Auto-generated method stub
		if (code == COMPLETE) {
			Toast.makeText(this, "Đăng kí thành công", Toast.LENGTH_LONG)
					.show();

			Intent intent = new Intent(RegisterActivity.this,
					LoginActivity.class);
			startActivity(intent);  
			finish();
		} else if (code == ERROR) {
			String message = (String) t;
			Toast.makeText(this, "Đăng kí thất bại: " + message,
					Toast.LENGTH_LONG).show();
		}
	}

}
